package com.dc.service.impl;

import com.dc.dao.ShoesDao;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Shoes;
import com.dc.service.ShoesService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class ShoesServiceImpl implements ShoesService {
    @Autowired
    ShoesDao shoesDao;
//分页查询商品信息
    public BaseModel selectPageShoes(Integer curr_page, Integer page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<Shoes> shoes = shoesDao.selectPageShoes();
        PageInfo pageInfo = new PageInfo(shoes,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分页查询用户信息成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel addShoes(Shoes shoes, BaseModel baseModel) throws Exception {
        int result = shoesDao.insertShoes(shoes);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_SHOES_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增成功");
        return baseModel;
    }

    public BaseModel deletedShoes(String ids, BaseModel baseModel) throws Exception {
        String[] idArry = ids.split(",");
        int result = shoesDao.deletedShoes(idArry);
        if(result <=0 ){
            throw new BusinessException(ErrorMessageEnum.DELETED_SHOES_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"条记录成功");
        return baseModel;
    }

    public BaseModel selectShoesById(int id, BaseModel baseModel) throws Exception {
        Shoes shoes = shoesDao.selectShoesById(id);
        if(shoes == null) {
            throw new BusinessException(ErrorMessageEnum.SELECT_SHOESBYID);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("根据id查询成功");
        baseModel.setData(shoes);
        return baseModel;
    }

    public BaseModel selectShoesByName(String shoes_name, Integer curr_page, Integer page_size,BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<Shoes> shoes = shoesDao.selectShoesByName(shoes_name);
        PageInfo pageInfo = new PageInfo(shoes,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分页查询用户信息成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel updateShoes(Shoes shoes,BaseModel baseModel) throws Exception {
        int result = shoesDao.updateShoes(shoes);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.UPDATE_SHOES);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改用户成功");
        return baseModel;
    }

    public BaseModel selectShoesByCategory(int category_id, Integer curr_page, Integer page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<Shoes> shoes = shoesDao.selectShoesByCategory(category_id);
        PageInfo pageInfo = new PageInfo(shoes,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分类查询用户信息成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }
    public BaseModel updateShoesImg(Shoes shoes, BaseModel baseModel) throws Exception {
        int result = shoesDao.updateShoesImg(shoes);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.UPDATE_SHOES);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改图片成功");
        return baseModel;
    }
}
